package com.koron.css2.meterService.impl;

import com.koron.css2.ImportExcelInterface;
import com.koron.css2.baseConfig.mapper.MeterBoreMapper;
import com.koron.css2.baseConfig.queryBean.MeterBoreQueryBean;
import com.koron.css2.baseConfig.vo.MeterBoreVO;
import com.koron.css2.common.dto.HeaderInfoDto;
import com.koron.css2.meterService.dto.MsChgPlanDetailDto;
import com.koron.css2.meterService.mapper.MsChgPlanDetailMapper;
import com.koron.css2.serviceManage.bean.RequestBean;
import com.koron.css2.serviceManage.bean.UserInfoBean;
import com.koron.util.Constant;
import com.koron.util.Tools;
import org.koron.ebs.mybatis.SessionFactory;
import org.springframework.web.multipart.MultipartFile;
import org.swan.bean.MessageBean;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * 换表增加模板导入
 * @author 张涛
 *
 */
public class MsChgAddImport implements ImportExcelInterface{

	@Override
	public MessageBean<?> importExel(SessionFactory factory, UserInfoBean userInfo, RequestBean req,List<Map<String, String>> data, MultipartFile file) {
		if (data==null){
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "请填写换表信息后导入", List.class);
		}
		MsChgPlanDetailMapper msChgPlanDetailMapper = factory.getMapper(MsChgPlanDetailMapper.class);
		MeterBoreMapper meterBoreMapper = factory.getMapper(MeterBoreMapper.class);
		List<String> userNos = data.stream().map(t -> t.get("userNo")).collect(Collectors.toList());
		List<MsChgPlanDetailDto> list = msChgPlanDetailMapper.selectListByUserNo(userNos);
		if (list==null || list.size()==0 || list.size()!=userNos.size()){
			return MessageBean.create(Constant.MESSAGE_INT_FAIL, "导入用户不存在或者用户无水表", List.class);
		}
		List<MeterBoreVO> meterBores = meterBoreMapper.selectMeterBoreList(new MeterBoreQueryBean());
		Map<String, String> meterForms = Tools.mapDicByCode(factory, "MMF"); //水表形态
		Map<String, String> handleWays = Tools.mapDicByCode(factory, "OMD"); //换表类型
		for(MsChgPlanDetailDto t : list) {
			MeterBoreVO meterBore = meterBores.stream().filter(k -> Objects.equals(t.getOldMeterBore(), k.getId())).findFirst().orElse(new MeterBoreVO());
			t.setOldMeterBore(meterBore.getBoreName());
			if(Objects.equals("1", t.getMeterForm())) {
				t.setTerm(meterBore.getNewChangeCycle());
			}
			if(Objects.equals("2", t.getMeterForm())) {
				t.setTerm(meterBore.getOldChangeCycle());
			}
			t.setMeterForm(meterForms.get(t.getMeterForm()));
			t.setOldHandleWayName(handleWays.get(t.getOldHandleWay()));
		}

		@SuppressWarnings("rawtypes")
		MessageBean message = MessageBean.create(Constant.MESSAGE_INT_SUCCESS, "导入成功", List.class);
		message.setData(list);
		return message;
	}

	@Override
	public List<HeaderInfoDto> getHeaderInfo(UserInfoBean userInfo) {
		List<HeaderInfoDto> headerInfos = new ArrayList<HeaderInfoDto>();
		headerInfos.add(new HeaderInfoDto("用户编号", false, "userNo"));
		headerInfos.add(new HeaderInfoDto("客户名称", false, "ctmName"));
		return headerInfos;
	}

	@Override
	public boolean isCheckHeader() {
		return true;
	}

}
